package com.pdstudio.weather;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.pdstudio.app.Application;
import com.pdstudio.bean.WeatherDetail;
import com.pdstudio.charts.LineChartView;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.util.TimeUtil;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherDetailActivity extends Activity {

	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	LinearLayout ly1;

	private Handler handler = null;
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 0) {
					List<WeatherDetail> wds = new ArrayList<WeatherDetail>();
					if (msg.obj != null) {
						try {
							wds = (List<WeatherDetail>) msg.obj;
							SetWeatherAdapter(wds);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		};

		setContentView(R.layout.weather_detail_main1);
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_weather_detail);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));
		

		customLiveIndexTitleView = findViewById(R.id.title_weather_detail);
		txtTitle = (TextView) customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("气象详情");
		
		ivBack = (ImageView) findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				WeatherDetailActivity.this.finish();

			}

		});


		ly1 = (LinearLayout) findViewById(R.id.linearlayout1);

		InitialData();

		/*
		 * WindowManager wm = this.getWindowManager(); // 因为有7天所以八等份 int width =
		 * wm.getDefaultDisplay().getWidth(); int perWidth = width / 7; int
		 * XPoint = perWidth / 2; // 原点的X坐标 int YPoint = (65 + 5) * 4; //
		 * 原点的Y坐标//显示-5到65度，小于0的全部放到-5处（65+5）*4放大四倍显示 int XScale = perWidth; //
		 * X的刻度长度 int YScale = 40; // Y的刻度长度 int XLength = width; // X轴的长度 int
		 * YLength = (65 + 5) * 4 + 20; // Y轴的长度 String[] XLabel = new String[]
		 * { "周一", "周二", "周三", "周四", "周五", "周六", "周日" }; // X的刻度 String[] YLabel
		 * = new String[] { "-5", "9", "23", "37", "51", "65" }; // Y的刻度
		 * String[] Data = new String[] { "-3", "23", "10", "36", "45", "40",
		 * "12" }; // 数据 String[] Data1 = new String[] { "10", "12", "30", "5",
		 * "40", "28", "12" }; // 数据 LineChartView lcv1 = new
		 * LineChartView(this, XPoint, YPoint, XScale, YScale, XLength, YLength,
		 * XLabel, YLabel, Data, Data1, "图标的标题"); // ly1.removeAllViews();
		 * ly1.addView(lcv1);
		 */
	}

	private void InitialData() {
		Thread thread = new Thread() {
			@Override
			public void run() {
				WeatherHttpMethod whm = new WeatherHttpMethod(
						WeatherDetailActivity.this);
				List<WeatherDetail> wds = whm.GetWeatherDetails(mSpUtil.getAuthCode(),TimeUtil.getSysDate(),mApplication.getCurSimpleCity().cityCode);
				Message msg = handler.obtainMessage(0, wds);
				handler.sendMessage(msg);
			}
		};
		thread.start();
	}

	/*
	 * 气象详情日期剪裁
	 */
	private String WeatherDetailDateCut(String date) {
		String result = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date d = df.parse(date);
			if (d != null) {
				Calendar c=Calendar.getInstance();
				c.setTime(d);
				result = (c.get(Calendar.MONTH)+1) + "/" + c.get(Calendar.DAY_OF_MONTH) + "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取天气的图标
	 */
	private int GetWeatherIcon(String climate) {
		int result = 0;
		try {
			result = mApplication.getWidgetWeatherIcon(climate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	private void SetWeatherAdapter(List<WeatherDetail> wds) {
		try {
			String[] Data = new String[6]; // 数据
			String[] Data1 = new String[6]; // 数据

			if (wds.size() > 0) {
				if (wds.get(0) != null) {
					try {
						TextView day = (TextView) findViewById(R.id.weather_detail_tv_yesterday);
						TextView day_date = (TextView) findViewById(R.id.weather_detail_tv_yesterday_date);
						TextView day_word = (TextView) findViewById(R.id.weather_detail_tv_yesterday_word);
						ImageView day_pic = (ImageView) findViewById(R.id.weather_detail_iv_yesterday_pic);
						
						day_pic.setImageResource(GetWeatherIcon(wds.get(0).weatherStatus));
						day.setText(wds.get(0).dayTitle);
						day_date.setText(WeatherDetailDateCut(wds.get(0).date));
						day_word.setText(wds.get(0).weatherStatus);
						Data[0] = wds.get(0).lowTemp;
						Data1[0] = wds.get(0).highTemp;
						
						//if(wds.get(0).weatherStatus.length()>2){
						//	day_word.setText(wds.get(0).weatherStatus.substring(0, 2));
						//}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if (wds.size() > 1) {
				if (wds.get(1) != null) {
					try {
						TextView day1 = (TextView) findViewById(R.id.weather_detail_tv_today);
						TextView day1_date = (TextView) findViewById(R.id.weather_detail_tv_today_date);
						TextView day1_word = (TextView) findViewById(R.id.weather_detail_tv_today_word);
						ImageView day1_pic = (ImageView) findViewById(R.id.weather_detail_iv_today_pic);
						day1_pic.setImageResource(GetWeatherIcon(wds.get(1).weatherStatus));
						day1.setText(wds.get(1).dayTitle);
						day1_date
								.setText(WeatherDetailDateCut(wds.get(1).date));
						day1_word.setText(wds.get(1).weatherStatus);
						Data[1] = wds.get(1).lowTemp;
						Data1[1] = wds.get(1).highTemp;
						
						//if(wds.get(1).weatherStatus.length()>2){
						//	day1_word.setText(wds.get(1).weatherStatus.substring(0, 2));
						//}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if (wds.size() > 2) {
				if (wds.get(2) != null) {
					try {
						TextView day2 = (TextView) findViewById(R.id.weather_detail_tv_tomorrow);
						TextView day2_date = (TextView) findViewById(R.id.weather_detail_tv_tomorrow_date);
						TextView day2_word = (TextView) findViewById(R.id.weather_detail_tv_tomorrow_word);
						ImageView day2_pic = (ImageView) findViewById(R.id.weather_detail_iv_tomorrow_pic);
						day2_pic.setImageResource(GetWeatherIcon(wds.get(2).weatherStatus));
						day2.setText(wds.get(2).dayTitle);
						day2_date
								.setText(WeatherDetailDateCut(wds.get(2).date));
						day2_word.setText(wds.get(2).weatherStatus);
						Data[2] = wds.get(2).lowTemp;
						Data1[2] = wds.get(2).highTemp;
						
						//if(wds.get(2).weatherStatus.length()>2){
						//	day2_word.setText(wds.get(2).weatherStatus.substring(0, 2));
						//}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if (wds.size() > 3) {
				if (wds.get(3) != null) {
					try {
						TextView day3 = (TextView) findViewById(R.id.weather_detail_tv_tomorrow1);
						TextView day3_date = (TextView) findViewById(R.id.weather_detail_tv_tomorrow1_date);
						TextView day3_word = (TextView) findViewById(R.id.weather_detail_tv_tomorrow1_word);
						ImageView day3_pic = (ImageView) findViewById(R.id.weather_detail_iv_tomorrow1_pic);
						day3_pic.setImageResource(GetWeatherIcon(wds.get(3).weatherStatus));
						day3.setText(wds.get(3).dayTitle);
						day3_date
								.setText(WeatherDetailDateCut(wds.get(3).date));
						day3_word.setText(wds.get(3).weatherStatus);
						Data[3] = wds.get(3).lowTemp;
						Data1[3] = wds.get(3).highTemp;
						
						//if(wds.get(3).weatherStatus.length()>2){
						//	day3_word.setText(wds.get(3).weatherStatus.substring(0, 2));
						//}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if (wds.size() > 4) {
				if (wds.get(4) != null) {
					try {
						TextView day4 = (TextView) findViewById(R.id.weather_detail_tv_tomorrow2);
						TextView day4_date = (TextView) findViewById(R.id.weather_detail_tv_tomorrow2_date);
						TextView day4_word = (TextView) findViewById(R.id.weather_detail_tv_tomorrow2_word);
						ImageView day4_pic = (ImageView) findViewById(R.id.weather_detail_iv_tomorrow2_pic);
						day4_pic.setImageResource(GetWeatherIcon(wds.get(4).weatherStatus));
						day4.setText(wds.get(4).dayTitle);
						day4_date
								.setText(WeatherDetailDateCut(wds.get(4).date));
						day4_word.setText(wds.get(4).weatherStatus);
						Data[4] = wds.get(4).lowTemp;
						Data1[4] = wds.get(4).highTemp;
						
						//if(wds.get(4).weatherStatus.length()>2){
						//	day4_word.setText(wds.get(4).weatherStatus.substring(0, 2));
						//}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if (wds.size() > 5) {
				if (wds.get(5) != null) {
					try {
						TextView day5 = (TextView) findViewById(R.id.weather_detail_tv_tomorrow3);
						TextView day5_date = (TextView) findViewById(R.id.weather_detail_tv_tomorrow3_date);
						TextView day5_word = (TextView) findViewById(R.id.weather_detail_tv_tomorrow3_word);
						ImageView day5_pic = (ImageView) findViewById(R.id.weather_detail_iv_tomorrow3_pic);
						day5_pic.setImageResource(GetWeatherIcon(wds.get(5).weatherStatus));
						day5.setText(wds.get(5).dayTitle);
						day5_date
								.setText(WeatherDetailDateCut(wds.get(5).date));
						day5_word.setText(wds.get(5).weatherStatus);
						Data[5] = wds.get(5).lowTemp;
						Data1[5] = wds.get(5).highTemp;
						
						//if(wds.get(5).weatherStatus.length()>2){
						//	day5_word.setText(wds.get(5).weatherStatus.substring(0, 2));
						//}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			/*
			 * if (wds.size() > 6) { if (wds.get(6) != null) {
			 * 
			 * TextView day6 = (TextView)
			 * findViewById(R.id.weather_detail_tv_tomorrow4); TextView
			 * day6_date = (TextView)
			 * findViewById(R.id.weather_detail_tv_tomorrow4_date); TextView
			 * day6_word = (TextView)
			 * findViewById(R.id.weather_detail_tv_tomorrow4_word); ImageView
			 * day6_pic = (ImageView)
			 * findViewById(R.id.weather_detail_iv_tomorrow4_pic);
			 * day6.setText(wds.get(6).dayTitle);
			 * day6_date.setText(wds.get(6).date);
			 * day6_word.setText(wds.get(6).weatherStatus); Data[6] =
			 * wds.get(6).lowTemp; Data[6] = wds.get(6).highTemp; } }
			 */
			try {
				WindowManager wm = this.getWindowManager();
				// 因为有7天所以八等份
				int width = wm.getDefaultDisplay().getWidth();
				int perWidth = width / 6;
				int XPoint = perWidth / 2; // 原点的X坐标
				int YPoint = (65 + 5) * 4; // 原点的Y坐标//显示-5到65度，小于0的全部放到-5处（65+5）*4放大四倍显示
				int XScale = perWidth; // X的刻度长度
				int YScale = 80; // Y的刻度长度
				int XLength = width; // X轴的长度
				int YLength = (65 + 5) * 4 + 20; // Y轴的长度
				String[] XLabel = new String[] { "周一", "周二", "周三", "周四", "周五",
						"周六" }; // X的刻度
				double minT=-10;
				double maxT=65;
				//算出最低最高温度
				for(int i=0;i<6;i++){
					try{
						double temp1=Double.parseDouble(Data1[i]);
						if(temp1>maxT){
							maxT=temp1;							
						}
						double temp=Double.parseDouble(Data[i]);
						if(temp<minT){
							minT=temp;
						}
					}catch(Exception e){e.printStackTrace();}
				}
				String[] YLabel = new String[] { "-5", "9", "23", "37", "51",
						"65" }; // Y的刻度
				//String[] YLabel=new String[]{"-5","0","5","10","15","20","25","30","35","40","45","50"};
				LineChartView lcv1 = new LineChartView(this, XPoint, YPoint,
						XScale, YScale, XLength, YLength, XLabel, YLabel, Data,
						Data1, "图标的标题");
				ly1.addView(lcv1);
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
